﻿package gov.jslt.app.plugin.signinfo;

import java.io.ByteArrayInputStream;
import java.security.MessageDigest;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;

public class SignInfo extends CordovaPlugin {

	private CallbackContext callbackContext = null;
	private JSONArray args = null;
	private JSONObject returnInfo;

	public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
		if ("getSignInfo".equals(action)) {
			return getSignInfo(action, args, callbackContext);
		}
		return false;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	public boolean getSignInfo(String action, JSONArray args, CallbackContext callbackContext)
			throws JSONException {
		this.callbackContext = callbackContext;
		this.args = args;
		try {
			PackageInfo packageInfo = cordova.getActivity().getPackageManager().getPackageInfo("gov.jslt.app", PackageManager.GET_SIGNATURES);
			Signature[] signs = packageInfo.signatures;
			Signature sign = signs[0];
			String certInfo = parseSignature(sign.toByteArray());
			String md5 = getMD5String(sign.toByteArray());
			returnInfo = new JSONObject();
			returnInfo.put("code", "0");
			returnInfo.put("msg", certInfo+"--md5:"+md5 );
			callbackContext.success(returnInfo);
		} catch (Exception e) {
			returnInfo = new JSONObject();
			returnInfo.put("code", "1");
			returnInfo.put("msg", "签名信息获取失败:" + e.getMessage());
			callbackContext.error(returnInfo);
		}
		return false;
	}
	
	public String parseSignature(byte[] signature) {
		try {
			CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
			X509Certificate cert = (X509Certificate) certFactory
					.generateCertificate(new ByteArrayInputStream(signature));
			String pubKey = cert.getPublicKey().toString();// 公钥
			String signNumber = cert.getSerialNumber().toString();// 证书序列号
			// String sigAlgName = cert.getSigAlgName();//签名算法名
			// String subjectDN = cert.getSubjectDN().toString();//地区 单位 别名等信息
			return "pubKey:" + pubKey + "--signNum:" + signNumber;
		} catch (CertificateException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static final String getMD5String(byte[] paramArrayOfByte) {
		char[] asciiTable = { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102 }; // ascii表对应的数字和字符的编码
		try {
			MessageDigest md5MessageDigest = MessageDigest.getInstance("MD5");
			md5MessageDigest.update(paramArrayOfByte);//
			byte[] tempByte = md5MessageDigest.digest();
			int i = tempByte.length;
			char[] tempChar = new char[i * 2];
			int j = 0;
			int k = 0;
			while (true) { // 将二进制数组转换成字符串
				if (j >= i) {
					return new String(tempChar);
				}
				int m = tempByte[j];
				int n = k + 1;
				tempChar[k] = asciiTable[(0xF & m >>> 4)];
				k = n + 1;
				tempChar[n] = asciiTable[(m & 0xF)];
				j++;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}